package vn.co.thn.web.demo_app_engine.model;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import vn.co.thn.web.demo_app_engine.PMF;
import vn.co.thn.web.demo_app_engine.jpa.Category;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;

public class CategoryModel {
	private BlobstoreService blobstoreService;
	public CategoryModel(){}
	public CategoryModel(BlobstoreService service){
		this.blobstoreService=service;
	}
	public void setBlobstoreService(BlobstoreService service){
		this.blobstoreService=service;
		
	}
	@SuppressWarnings("unchecked")
	public synchronized List<Category> getAll() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<Category> list = new ArrayList<Category>();
		
		try {
			tx.begin();

			Query q = pm.newQuery(Category.class);
			q.setFilter("Delete == 1");
			list = (List<Category>) q.execute();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}

		return list;
	}

	public synchronized void delete(String key) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
//		Transaction tx = pm.currentTransaction();
		Category category = pm.getObjectById(Category.class, key);
		category.setDelete(9);
		try {
//			tx.begin();
			pm.makePersistent(category);
		} finally {
//			if (tx.isActive()) {
//				tx.rollback();
//			}
			pm.close();
		}
		this.deleteImage(category.getKey_image());
	}

	public synchronized void add(Category category) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			if (getListDelete().size() > 0) {
				category.setId(getListDelete().get(0).getId());
			} else {
				category.setId(getNewKey());
			}
			category.CreateKey();
			pm.makePersistent(category);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	private List<Category> getListDelete() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<Category> list = new ArrayList<Category>();
		try {
			tx.begin();

			Query q = pm.newQuery(Category.class);
			q.setFilter("Delete == 9");
			list = (List<Category>) q.execute();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	private int getNewKey() {
		int key;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<Category> list = new ArrayList<Category>();
		try {
			tx.begin();

			Query q = pm.newQuery(Category.class);
			q.setOrdering("Id desc");
			list = (List<Category>) q.execute();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		if (list.size() > 0)
			key = list.get(0).getId() + 1;
		else
			key = 1;
		return key;
	}
	private void deleteImage(String key_image){
		if(key_image!=null){
		 BlobKey blobKey = new BlobKey(key_image);
		blobstoreService.delete(blobKey);
		}
	}
}
